<template>
  <div class="wrap">
    <div class="login_wrap">
      <div class="login_img">
        <img src="../../../public/下载.png" alt="" />
      </div>
      <div class="login_phone">
        <van-field v-model="phone" placeholder="请输入手机号" class="field" />
        <van-field
          v-model="codeAuth"
          center
          clearable
          placeholder="请输入短信验证码"
        >
          <template #button>
            <div size="small" class="login_auth" @click="auths">发送验证码</div>
          </template>
        </van-field>
      </div>
      <van-button type="danger" class="login_btn" @click="login"
        >立即登录</van-button
      >
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      phone: "",
      auth: "",
      codeAuth: "",
    };
  },
  methods: {
    login() {
      if (this.phone == "" && this.codeAuth == "") {
        this.$toast("输入框不能为空");
      } else {
        if (this.phone != "" && this.codeAuth == this.auth) {
          axios.post("/api/login", { phone: this.phone }).then((res) => {
            console.log(res.data.phone);
            localStorage.setItem("token", res.data.phone);
            if (this.$route.query.name) {
              this.$router.push(this.$route.query.name);
            } else {
              this.$router.push({
                path: this.$route.query.r || "/",
              });
            }
          });
        }
      }
    },
    auths() {
      axios.post("/api/auth", { authPhone: this.phone }).then((res) => {
        console.log(res);
        this.auth = res.data.auth;
        this.$toast("验证码已发送");
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_wrap {
  width: 7.5rem;
  .login_img {
    padding: 3rem 0 0 3rem;
  }
  .login_phone {
    width: 6rem;
    margin: 0.6rem 0 0 0.7rem;
    .van-cell {
      border-bottom: 0.01rem solid #ccc;
    }
    .login_auth {
      border: none;
      color: red;
      margin-top: 0.2rem;
      font-size: 0.3rem;
    }
  }
  .login_btn {
    width: 6rem;
    margin-left: 0.7rem;
    border-radius: 0.1rem;
  }
}
</style>